package com.pansoft.openplanet.util;

import android.app.Dialog;
import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.chat.http.GetHttpUtil;
import com.efounder.chat.http.OpenPlanetFriendUserRequestUtil;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.utils.Constants;
import com.efounder.mobilecomps.contacts.User;
import com.efounder.util.AppContext;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.utilcode.util.SizeUtils;

import org.json.JSONException;

import java.util.List;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

//同步联系人相关网络请求
public class SyncImUserManager {
    private Context mContext;
    private AlertDialog alertDialog;
    AlertDialog.Builder builder;
    private TextView messageView;
    private Button enterButton;
    private Button cancelButton;

    public SyncImUserManager(Context context) {
        this.mContext = context;
        builder = new AlertDialog.Builder(context);
    }

    public void requestServer() throws Exception {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }


        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.open_plant_syncuser_dialog_layout, null);
        messageView = layout.findViewById(R.id.tvmessage);
        enterButton = layout.findViewById(R.id.btn_ok);
        cancelButton = layout.findViewById(R.id.btn_cancel);
        builder.setView(layout);

        alertDialog = builder.create();
        setSize(alertDialog);
        // alertDialog.setCancelable(false);
        alertDialog.show();
//        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
//        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);

        cancelButton.setVisibility(View.INVISIBLE);
        enterButton.setVisibility(View.INVISIBLE);
        messageView.setText(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_link));

        GetHttpUtil.getUserInfo(Integer.valueOf(EnvironmentVariable.getProperty(CHAT_USER_ID)),
                mContext, new GetHttpUtil.GetUserListener() {
                    @Override
                    public void onGetUserSuccess(User user) {
                        EnvironmentVariable.setProperty(Constants.KEY_NICK_NAME, user.getNickName());
                    }

                    @Override
                    public void onGetUserFail() {

                    }
                });
        OpenPlanetFriendUserRequestUtil.getFriendData(mContext, new GetHttpUtil.ReqCallBack<String>() {
            @Override
            public void onReqSuccess(String result) {
                if ("success".equals(result)) {
                    //同步部落信息
                    messageView.setText(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_group));

                    try {
                        GetHttpUtil.getGroupListByLoginId(mContext, new GetHttpUtil.ReqCallBack<List<Integer>>() {
                            @Override
                            public void onReqSuccess(List<Integer> result) {
                                messageView.setText(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_application));
                                try {
                                    GetHttpUtil.getAllOfficialNumber(mContext, new GetHttpUtil.ReqCallBack() {
                                        @Override
                                        public void onReqSuccess(Object result) {
                                            messageView.setText(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_complete));
                                            EnvironmentVariable.setProperty(EnvironmentVariable.getProperty(CHAT_USER_ID) + "tcfirstrequest", "false");

//                                            alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
//                                            alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
//                                                @Override
//                                                public void onClick(View v) {
//                                                    alertDialog.dismiss();
//                                                }
//                                            });
                                            alertDialog.dismiss();
                                            return;
//                                            cancelButton.setVisibility(View.INVISIBLE);
//                                            enterButton.setVisibility(View.VISIBLE);
//                                            enterButton.setOnClickListener(new View.OnClickListener() {
//                                                @Override
//                                                public void onClick(View v) {
//                                                    alertDialog.dismiss();
//                                                }
//                                            });
                                        }

                                        @Override
                                        public void onReqFailed(String errorMsg) {
                                            changedFailState(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_group_fail));
                                        }
                                    });
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onReqFailed(String errorMsg) {
                                changedFailState(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_app_fail));
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                changedFailState(ResStringUtil.getString(R.string.open_planet_asset_synclm_user_updating_link_fail));
            }
        });

    }

    private void changedFailState(String msg) {
        messageView.setText(msg);

//        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
//        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                try {
//                    requestServer();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
        cancelButton.setVisibility(View.VISIBLE);
        enterButton.setVisibility(View.INVISIBLE);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    requestServer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private ViewGroup.LayoutParams setSize(Dialog dialog) {
        //设置dialog的大小
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		    /* lp.width = Detail_Util.dip2px(appContext, bar.getWidth())+450;
		    lp.height =Detail_Util.dip2px(appContext,  bar.getHeight())+350;*/
        DisplayMetrics dm = new DisplayMetrics();
        //屏幕分辨率容器
        WindowManager windowManager = (WindowManager) AppContext.getInstance()
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;
        lp.width = (int) (screenWidth / 1.2);
        //  lp.height = screenHeight/3;
        lp.height = SizeUtils.dp2px(150);
        dialogWindow.setAttributes(lp);
        return lp;
    }

}
